﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using Microsoft.ApplicationBlocks.Data;

namespace ModuleDuoc
{
    public partial class frmNiemYetGia : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        string _manhanvien = "Nguyễn Thế Sơn";
        string _maphongban;

        public frmNiemYetGia()
        {
            InitializeComponent();
        }
        public frmNiemYetGia(string tennhanvien, string maphongban)
        {
            InitializeComponent();
            _manhanvien = tennhanvien;
            _maphongban = maphongban;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="where"></param>
        private void getThuoc(string where)
        {
            string sql = "select mavt,tenvt from zD_DM_THUOC" + where;
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                dgvThuoc.DataSource = dt;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmNiemYetGia_Load(object sender, EventArgs e)
        {
            this.dgvGia.AutoGenerateColumns = false;
            this.dgvThuoc.AutoGenerateColumns = false;
            getThuoc("");
            getNhomThuoc();
        }
        private void getNhomThuoc()
        {
            string sql = "select MANHOM,TENNHOM from zD_DM_VATTU_NHOMVATTU union select '',N'--Tất cả--'";
            DataTable dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            this.cboNhomThuoc.DataSource = dt;
            this.cboNhomThuoc.DisplayMember="TENNHOM";
            this.cboNhomThuoc.ValueMember = "MANHOM";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvThuoc_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //try
            //{
                string sql = "select * from view_THUOC_PHIEUNHAPKHOCT where mavt='" + dgvThuoc["mavt", dgvThuoc.CurrentRow.Index].Value.ToString() + "' and source='new'";
                DataTable dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
                //if (dt.Rows.Count > 0)
                //{
                    dgvGia.DataSource = dt;
                //}
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("Lỗi:" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
        }

        /// <summary>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtKeyword_TextChanged(object sender, EventArgs e)
        {
            getThuoc(" where tenvt like N'%" + this.txtKeyword.Text.Trim() + "%'");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvGia_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string sql = "update zD_PHIEUNHAPKHOCT set giaban='" + dgvGia["giaban", dgvGia.CurrentRow.Index].Value.ToString() + "',checked = 1,nguoicapnhat=N'" + _manhanvien + "',ngaycapnhat='" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "'  where mavach='" + dgvGia["_mavach", dgvGia.CurrentRow.Index].Value.ToString() + "'";
                SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql);
                this.dgvGia.Rows[dgvGia.CurrentRow.Index].DefaultCellStyle.BackColor = Color.White;
                this.dgvGia.Rows[dgvGia.CurrentRow.Index].Cells["update"].Value = "Đã cập nhật";
                this.dgvGia.Rows[dgvGia.CurrentRow.Index].Cells["ngaycapnhat"].Value = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss").ToString();
                this.dgvGia.Rows[dgvGia.CurrentRow.Index].Cells["nguoicapnhat"].Value = _manhanvien;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Giá nhập chưa đúng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dgvGia["giaban", dgvGia.CurrentRow.Index].Selected = true;
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radXemTatCa_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                string sql = "select * from view_THUOC_PHIEUNHAPKHOCT where source='new' order by mavach desc";
                DataTable dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    dgvGia.DataSource = dt;
                }
                for (int i = 0; i < dgvGia.Rows.Count; i++)
                {
                    if (int.Parse(dgvGia.Rows[i].Cells["_checked"].Value.ToString()) == 0)
                    {
                        dgvGia.Rows[i].DefaultCellStyle.BackColor = Color.Azure;
                        this.dgvGia.Rows[i].Cells["update"].Value = "Chưa cập nhật giá";
                    }
                    else
                    {
                        this.dgvGia.Rows[i].Cells["update"].Value = "Đã cập nhật";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi:" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radXemTheoTen_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                string sql = "select * from view_THUOC_PHIEUNHAPKHOCT where mavt='" + dgvThuoc["mavt", dgvThuoc.CurrentRow.Index].Value.ToString() + "' and source='new'";
                DataTable dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    dgvGia.DataSource = dt;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi:" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cboNhomThuoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string sql = "";
                if (cboNhomThuoc.SelectedValue.ToString() != "")
                {

                    sql = "select * from view_THUOC_PHIEUNHAPKHOCT where source='new' and MANHOMVATTU='" + cboNhomThuoc.SelectedValue.ToString() + "' order by mavach desc";
                }
                else
                {
                    sql = "select * from view_THUOC_PHIEUNHAPKHOCT where source='new' order by mavach desc";
                }

                DataTable dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    dgvGia.DataSource = dt;
                }
                for (int i = 0; i < dgvGia.Rows.Count; i++)
                {
                    if (int.Parse(dgvGia.Rows[i].Cells["_checked"].Value.ToString()) == 0)
                    {
                        dgvGia.Rows[i].DefaultCellStyle.BackColor = Color.Azure;
                        this.dgvGia.Rows[i].Cells["update"].Value = "Chưa cập nhật giá";
                    }
                    else
                    {
                        this.dgvGia.Rows[i].Cells["update"].Value = "Đã cập nhật";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi:" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void radGiaZero_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                string sql = "select * from view_THUOC_PHIEUNHAPKHOCT where source='new' and gianhap=0 order by mavach desc";
                DataTable dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    dgvGia.DataSource = dt;
                }
                for (int i = 0; i < dgvGia.Rows.Count; i++)
                {
                    if (int.Parse(dgvGia.Rows[i].Cells["_checked"].Value.ToString()) == 0)
                    {
                        dgvGia.Rows[i].DefaultCellStyle.BackColor = Color.Azure;
                        this.dgvGia.Rows[i].Cells["update"].Value = "Chưa cập nhật giá";
                    }
                    else
                    {
                        this.dgvGia.Rows[i].Cells["update"].Value = "Đã cập nhật";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi:" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
